<?php

/**
 * Maikuro CMS
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

require_once 'entity/SettingEntity.php';

class SettingService extends AbstractDbEntityService
{
    protected $cacheIdentifier = 'settings_cache',
              $dbTable         = 'setting',
              $entityClass     = 'SettingEntity';

    /**
     * Returns the setting with the specified name.
     *
     * @param string $name
     * @return StdClass
     */
    public function getSetting($name)
    {
        if (!isset($this->entities[$name])) {
            throw new Exception('Unknown setting.');
        }
        return $this->entities[$name];
    }

    /**
     * Returns all settings.
     *
     * @return array
     */
    public function getSettings()
    {
        $settings = array();
        foreach ($this->entities as $setting) {
            $settings[$setting->id] = clone $setting;
        }
        return $settings;
    }

    /**
     * Returns all the settings as an associative array with setting names as
     * array keys and setting values as array values.
     *
     * @return array
     */
    public function getSettingsArray()
    {
        $settingsArray = array();
        foreach ($this->entities as $setting) {
            $settingsArray[$setting->name] = $setting->value;
        }
        return $settingsArray;
    }

    /**
     * Returns the setting value of the specified setting name.
     *
     * @param string $setting
     * @return string
     */
    public function getSettingValue($setting)
    {
        return $this->getSetting($setting)->value;
    }

    /**
     * Loads the settings from the database.
     *
     * @return void
     */
    protected function fetchEntities()
    {
        $result = $this->pdo->select()
                            ->from($this->dbTable)
                            ->fetchAllAs($this->entityClass);
        foreach ($result as $setting) {
            $this->entities[$setting->name] = $setting;
        }
    }

    /**
     *
     * @param array $settingsArray
     * @return void
     */
    public function updateValuesFromArray(array $settingsArray)
    {
        foreach ($settingsArray as $name => $value) {
            $this->pdo->update($this->dbTable, array('value' => $value), 'name = ?', $name);
        }
    }
}